/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.mainApp;

import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JPanel;

import org.newdawn.slick.SlickException;

import ca.usherbrooke.behaviours.BasicBehaviour;
import ca.usherbrooke.mainApp.view.simulation.PanelSimulation;
import ca.usherbrooke.utils.ExceptionTool;
import ca.usherbrooke.view.properties.ViewSlickProperties;
import ca.usherbrooke.view.slick.jade.IWorldProvider;

/**
 * @author Cody Stoutenburg
 * 
 */
public class StartGlobalViewBehaviour extends BasicBehaviour<WorldAgentViewer> {
	private static final long serialVersionUID = 1L;

	private final IWorldProvider worldProvider;
	private final String propertyPath;
	private Boolean isDone;

	public StartGlobalViewBehaviour(WorldAgentViewer a, IWorldProvider world,
			String propertyPath) {
		super(a);
		this.worldProvider = world;
		this.propertyPath = propertyPath;
		isDone = false;
	}

	@Override
	public void action() {
		String path = (String) this.getDataStore().get(propertyPath);

		if (path != null && this.getAgent().getWorld() != null) {
			setPropertyPath(path);
			isDone = true;
		}
	}

	@Override
	public boolean done() {
		return isDone;
	}

	public void setPropertyPath(String path) {
		JPanel centerPane = MainAppAgent.APP_FRAME.getMainPanel()
				.getCenterPanel();

		centerPane.removeAll();
		centerPane.validate();

		try {
			ViewSlickProperties properties = new ViewSlickProperties(path);
			PanelSimulation simu = new PanelSimulation(this.getAgent(),
					worldProvider, this.getAgent().getWorldAgent(), properties);
			centerPane.add(simu);
			simu.start();

			centerPane.revalidate();
			centerPane.repaint();
		} catch (FileNotFoundException ex) {
			ExceptionTool.showError(
					"Property file not found the view will be deleted", ex);
			this.getAgent().doDelete();
		} catch (IOException ex) {
			ExceptionTool.showError(
					"Property file not found the view will be deleted", ex);
			this.getAgent().doDelete();
		} catch (SlickException ex) {
			ExceptionTool.showError(
					"Property file not found the view will be deleted", ex);
			this.getAgent().doDelete();
		}
	}
}
